/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListModel;
import org.swirrel.annotation.ContentsChanged;
import org.swirrel.annotation.IntervalAdded;
import org.swirrel.annotation.IntervalRemoved;
import org.swirrel.listener.SwirrelListDataListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class ListDataListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public ListDataListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(ContentsChanged.class,
                IntervalAdded.class, IntervalRemoved.class),
                Arrays.<Class<?>>asList(JList.class, JComboBox.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
        //No way to get ListDataListeners from a ListModel...
        return false;
    }

    public void registerListener(Field field, Component comp, List<MethodCall> methodCalls) throws IllegalAccessException {
        Object obj = field.get(comp);
        ListModel model = (obj instanceof JList) ? ((JList)obj).getModel() : ((JComboBox)obj).getModel();
        model.addListDataListener(new SwirrelListDataListener(methodCalls.get(0),
                methodCalls.get(1), methodCalls.get(2)));
    }
}
